/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proyectocatalina;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JProgressBar;
/**
 * @web http://jc-mouse.blogspot.com/
 * @author Mouse
 */
public class BackUpSample{
    private int BUFFER = 10485760;
    //para guardar en memmoria
    private StringBuffer temp = null;
    //para guardar el archivo SQL
    private FileWriter  fichero = null;
    private PrintWriter pw = null;
    String host;
    String usuario;
    String password;
    String bd;
    String rutabck;
    public BackUpSample(String host1,String usuario1,String password1,String rutabck1,String bd2){
        host = host1;
        usuario = usuario1;
        password = password1;
        rutabck = rutabck1; 
        bd = bd2;
    }
    public boolean CrearBackup(){
    boolean ok=false;
    try{
        //sentencia para crear el BackUp
         Process run = Runtime.getRuntime().exec(
        "mysqldump --host=" + host + " --port=" + "3306" +
        " --user=" + usuario+ " --password=" + password +
        " --compact --complete-insert --extended-insert --skip-quote-names" +
        " --skip-triggers " +"-p "+ bd +" --routines");
        //se guarda en memoria el backup
        InputStream in = run.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        temp = new StringBuffer();
        int count;
        char[] cbuf = new char[BUFFER];
        while ((count = br.read(cbuf, 0, BUFFER)) != -1)
            temp.append(cbuf, 0, count);
        br.close();
        in.close();
        /* se crea y escribe el archivo SQL */
        fichero = new FileWriter(rutabck);
        pw = new PrintWriter(fichero);
        pw.println(temp.toString());
        ok=true;
   }
    catch (Exception ex){
            ex.printStackTrace();
    } finally {
       try {
         if (null != fichero)
              fichero.close();
       } catch (Exception e2) {
           e2.printStackTrace();
       }
    }
    return ok;
 }

    }
 
